var express = require('express');
var router = express.Router();
const sql = require('../db')
    // const Code = require("./routes/code")
    // app.get('/code', (req, res) => {
    //     let mobile = req.query.mobile
    //     console.log(mobile);
    //     Code(res, mobile)
    // })

/* GET home page. */
router.get('/pic', (req, res) => {
        // req.query req.boby post req.paramms
        // if (!isNaN(req.params.username)) {
        sql("select * from userteeth", {})
            .then((result) => {
                res.json({ code: 200, data: result });
            }).catch(() => {
                res.json({ code: 500, data: "程序出错" });
            })
    }
    //  else {
    //     res.json({ code: 400, data: "用户名不正确" })
    // }
)

//查询某一天的刷牙记录
router.get('/:date', (req, res) => {
    // req.query req.boby post req.paramms
    if (!isNaN(req.params.date)) {
        sql("select * from month where date=?", req.params.date)
            .then((result) => {
                res.json({ code: 200, data: result[0] });
            }).catch(() => {
                res.json({ code: 400, data: "编号不正确" });
            })
    } else {
        res.json({ code: 500, data: "程序出错" })
    }
})

//新增刷牙记录
router.post('/m/month', (req, res) => {
        if (req.body) {
            sql("insert into month set ?", req.body).then((result) => {
                if (result.affectedRows > 0) {
                    res.json({ code: 200, data: "新增成功" });
                } else {
                    res.json({ code: 400, data: "新增失败" })
                }
            }).catch(() => {
                res.json({ code: 500, data: "程序出错" });
            })
        }
    })
    //删除刷牙记录
router.delete('/:date', (req, res) => {
    sql('delete from month where date=?', req.params.date)
        .then((result) => {
            if (result.affectedRows > 0) {
                res.json({ code: 200, data: "删除用户成功" })
            } else {
                res.json({ code: 400, data: "删除用户失败" })
            }
        }).catch((err) => {
            res.json({ code: 500, data: "程序出错" })
        })
})
router.get('/s/month', function(req, res, next) {

    sql('select * from month', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/y/ymonth', function(req, res, next) {
    sql('select * from ymonth', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/t/tmonth', function(req, res, next) {
    sql('select * from tmonth', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/y/yquarter', function(req, res, next) {
    sql('select * from yquarter', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/s/quarters', function(req, res, next) {
    sql('select * from quarters', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
router.get('/t/tquarter', function(req, res, next) {
    sql('select * from tquarter', [])
        .then((result) => {
            res.json({ code: 200, data: result });
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序错误" })
        })
});
//修改刷牙记录
router.put("/:date", (req, res) => {
    sql(`update month set ? where date=? `, [{ score: req.body.score, 均匀率: req.body.均匀率, 压感: req.body.压感 }, req.params.date])
        .then((result) => {
            if (result.affectedRows > 0) {
                res.json({ code: 200, data: "修改成功" })
            } else {
                res.json({ code: 400, data: "修改失败" })
            }
        }).catch((err) => {
            console.log(err);
            res.json({ code: 500, data: "程序出错" })
        })
})
module.exports = router;